
def is_prime(a):
    if a <= 1: return False
    for i in range(2, int(a**0.5) + 1):
        if a % i == 0: return False
    return True

def max_prime(b):
    if len(str(b)) != 4 or b < 1000: return 0
    c = str(b)
    e = 0
    for i in range(4):
        for j in range(i + 1, 5):
            d = int(c[i:j])
            if is_prime(d) and d > e: e = d
    return e

b = int(input())
r = max_prime(b)
print(r)

